//
//  PostSectionHeaderView.m
//  BaseProject
//
//  Created by 王洋 on 2020/12/10.
//  Copyright © 2020 王洋. All rights reserved.
//

#import "PostSectionHeaderView.h"

@implementation PostSectionHeaderView

+(CGFloat)calculateHWithModel:(CommonPostModel *)model {
    
    CGFloat topPading = 0;//距离顶部的距离
    CGFloat userInfoHeight = 60;//用户信息高度
    CGFloat labHeight = model.textHeight;//文字高度可计算
    CGFloat labBottomDistance = 10;//文字高度可计算

    CGFloat imgContent = model.imageHeight;//计算这个来控制宽度
    CGFloat actionHeight = 30;
    
    CGFloat bottomHeight;
    
    if (model.sysPostChildList.count > 0) {
        bottomHeight = 10;
    } else {
        bottomHeight = CGFLOAT_MIN;
    }
    
    return topPading + userInfoHeight + labHeight + labBottomDistance + imgContent + actionHeight + bottomHeight;
}

- (void)awakeFromNib {
    [super awakeFromNib];
    
    self.userIcon.userInteractionEnabled = YES;
    [self.userIcon addGestureTapEventHandle:^(id  _Nonnull sender, UITapGestureRecognizer * _Nonnull gestureRecognizer) {
        if (self.PostSectionHeaderViewBlock) {
            self.PostSectionHeaderViewBlock(PostSectionHeaderActionType_userIcon, 0);
        }
    }];
    
    [self.contentImg1 addGestureTapEventHandle:^(id  _Nonnull sender, UITapGestureRecognizer * _Nonnull gestureRecognizer) {
        if (self.PostSectionHeaderViewBlock) {
            self.PostSectionHeaderViewBlock(PostSectionHeaderActionType_image, 0);
        }
    }];
    
    [self.contentImg2 addGestureTapEventHandle:^(id  _Nonnull sender, UITapGestureRecognizer * _Nonnull gestureRecognizer) {
        if (self.PostSectionHeaderViewBlock) {
            self.PostSectionHeaderViewBlock(PostSectionHeaderActionType_image, 1);
        }
    }];
    
    [self.contentImg3 addGestureTapEventHandle:^(id  _Nonnull sender, UITapGestureRecognizer * _Nonnull gestureRecognizer) {
        if (self.PostSectionHeaderViewBlock) {
            self.PostSectionHeaderViewBlock(PostSectionHeaderActionType_image, 2);
        }
    }];
    
}

- (void)layoutSubviews {
    [super layoutSubviews];
    
//    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.01 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [self.userIcon pp_clipCorners:UIRectCornerAllCorners radius:self.userIcon.frame.size.height/2];

        [self.contentImg1 pp_clipCorners:UIRectCornerAllCorners radius:8];
        [self.contentImg2 pp_clipCorners:UIRectCornerAllCorners radius:8];
        [self.contentImg3 pp_clipCorners:UIRectCornerAllCorners radius:8];
        [self.contentImg3View pp_clipCorners:UIRectCornerAllCorners radius:8];

//    });
}

- (void)fillWithModel:(CommonPostModel *)model {
    
    NSInteger imgShowCount = 0;
    if (model.sysMediaList.count == 0) {
        imgShowCount = 0;
    } else if (model.sysMediaList.count <= 3) {
        imgShowCount = model.sysMediaList.count;
    } else {
        imgShowCount = 3;
    }
        
    switch (imgShowCount) {
        case 0:{
            
            [self imagesLayOut];
            self.contentImg1.hidden = YES;
            self.contentImg2.hidden = YES;
            self.contentImg3.hidden = YES;
            self.contentImg3View.hidden = YES;
            
        } break;
        case 1:{
            
            [self simpleImageLayout];
            self.contentImg1.hidden = NO;
            self.contentImg2.hidden = YES;
            self.contentImg3.hidden = YES;
            self.contentImg3View.hidden = YES;
            
        } break;
        case 2:{
            
            [self imagesLayOut];
            self.contentImg1.hidden = NO;
            self.contentImg2.hidden = NO;
            self.contentImg3.hidden = YES;
            self.contentImg3View.hidden = YES;
            
        } break;
        case 3:{
            
            [self imagesLayOut];
            self.contentImg1.hidden = NO;
            self.contentImg2.hidden = NO;
            self.contentImg3.hidden = NO;
            self.contentImg3View.hidden = NO;
            
        } break;
        default:{
            
            [self imagesLayOut];
            self.contentImg1.hidden = YES;
            self.contentImg2.hidden = YES;
            self.contentImg3.hidden = YES;
            self.contentImg3View.hidden = YES;
            
        } break;
    }
    
    WEAKSELF
    if (model.sysMediaList.count > 0) {
        NSString *url = [TT_OSSManager reSourceUrl:model.sysMediaList[0].url type:BA_resizeType_pro];
        [self.contentImg1 sd_setImageWithURL:[NSURL URLWithString:url] placeholderImage:[UIImage imageNamed:@"placeholder"] completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
            if (image) {
                if (cacheType != SDImageCacheTypeMemory) {
                    [UIView transitionWithView:weakSelf.contentImg1 duration:0.61 options:UIViewAnimationOptionTransitionCrossDissolve animations:^{
                        [weakSelf.contentImg1 setImage:image];
                        weakSelf.contentImg1.alpha = 1.0;
                    } completion:nil];
                } else {
                    weakSelf.contentImg1.alpha = 1.0;
                    [weakSelf.contentImg1 setImage:image];
                }
            }
        }];
    }
    
    if (model.sysMediaList.count > 1) {
        NSString *url = [TT_OSSManager reSourceUrl:model.sysMediaList[1].url type:BA_resizeType_pro];
        [self.contentImg2 sd_setImageWithURL:[NSURL URLWithString:url] placeholderImage:[UIImage imageNamed:@"placeholder"] completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
            if (image) {
                if (cacheType != SDImageCacheTypeMemory) {
                    [UIView transitionWithView:weakSelf.contentImg2 duration:0.61 options:UIViewAnimationOptionTransitionCrossDissolve animations:^{
                        [weakSelf.contentImg2 setImage:image];
                        weakSelf.contentImg2.alpha = 1.0;
                    } completion:nil];
                } else {
                    weakSelf.contentImg2.alpha = 1.0;
                    [weakSelf.contentImg2 setImage:image];
                }
            }
        }];
    }
    
    if (model.sysMediaList.count > 2) {
        NSString *url = [TT_OSSManager reSourceUrl:model.sysMediaList[2].url type:BA_resizeType_pro];
        [self.contentImg3 sd_setImageWithURL:[NSURL URLWithString:url] placeholderImage:[UIImage imageNamed:@"placeholder"] completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
            if (image) {
                if (cacheType != SDImageCacheTypeMemory) {
                    [UIView transitionWithView:weakSelf.contentImg3 duration:0.61 options:UIViewAnimationOptionTransitionCrossDissolve animations:^{
                        [weakSelf.contentImg3 setImage:image];
                        weakSelf.contentImg3.alpha = 1.0;
                    } completion:nil];
                } else {
                    weakSelf.contentImg3.alpha = 1.0;
                    [weakSelf.contentImg3 setImage:image];
                }
            }
        }];
    }
    
    self.postContentHeight.constant = model.textHeight;
    self.imageContentHeight.constant = model.imageHeight;

    if (model.sysMediaList.count>3) {
        self.contentImg3View.hidden = NO;
        self.contentImg3Lab.text = [NSString stringWithFormat:@"+%u",model.sysMediaList.count-3];
    } else {
        self.contentImg3View.hidden = YES;
        self.contentImg3Lab.text = @"";
    }
    
    [self layoutIfNeeded];
    
    if (model.isZan.length > 0) {
        [self.postThumbBtn setImage:[UIImage imageNamed:@"tanhun_zan_sel"] forState:UIControlStateNormal];
        [self.postThumbBtn setTitleColor:UIColorHex(0xFF4242) forState:UIControlStateNormal];
    } else {
        [self.postThumbBtn setImage:[UIImage imageNamed:@"tanhun_zan"] forState:UIControlStateNormal];
        [self.postThumbBtn setTitleColor:UIColorHex(0x333333) forState:UIControlStateNormal];
    }
    
    
    [self.userIcon sd_setImageWithURL:[NSURL URLWithString:model.userHeadUrl]];
    self.userNameLab.text = model.userNickName;
    self.postTimeLab.text = model.createTime;
    self.postContentLab.text = model.content;
    
    [self.postTagBtn setTitle:[NSString stringWithFormat:@" %@",model.themeTitle] forState:UIControlStateNormal];
    [self.postEvaBtn setTitle:[NSString stringWithFormat:@" %ld",model.replyCount] forState:UIControlStateNormal];
    [self.postThumbBtn setTitle:[NSString stringWithFormat:@" %ld",model.likes] forState:UIControlStateNormal];

}

//单图的约束
- (void)simpleImageLayout {
    [NSLayoutConstraint deactivateConstraints:@[self.contentImg1Scale]];
    self.contentImg1Scale = [NSLayoutConstraint constraintWithItem:self.contentImg1 attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:self.contentImg1 attribute:NSLayoutAttributeHeight multiplier:1.4583 constant:0];
    [NSLayoutConstraint activateConstraints:@[self.contentImg1Scale]];

}

//多图的约束
- (void)imagesLayOut {
    [NSLayoutConstraint deactivateConstraints:@[self.contentImg1Scale]];
    self.contentImg1Scale = [NSLayoutConstraint constraintWithItem:self.contentImg1 attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:self.contentImg1 attribute:NSLayoutAttributeHeight multiplier:1 constant:0];
    [NSLayoutConstraint activateConstraints:@[self.contentImg1Scale]];
}

//评价按钮
- (IBAction)evaluateAction:(id)sender {
    if (self.PostSectionHeaderViewBlock) {
        self.PostSectionHeaderViewBlock(PostSectionHeaderActionType_eva, -1);
    }

}

//点赞按钮
- (IBAction)thumbAction:(id)sender {
    if (self.PostSectionHeaderViewBlock) {
        self.PostSectionHeaderViewBlock(PostSectionHeaderActionType_thumb, -1);
    }
}

- (IBAction)goTheme:(id)sender {
    if (self.PostSectionHeaderViewBlock) {
        self.PostSectionHeaderViewBlock(PostSectionHeaderActionType_theme, -1);
    }
}



@end
